﻿using AppService.IService.System_setting;
using Domain.Entity.System_setting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers.System_setting
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CompanyController : ControllerBase
    {

        public ICompanyService company { get; set; }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Upload()
        {
            // 检查请求是否包含有效的文件数据
            if (!Request.HasFormContentType || !Request.Form.Files.Any())
            {
                return BadRequest("无效的请求，请确保包含有效的文件数据。");
            }

            // 获取上传的文件
            var file = Request.Form.Files[0];

            // 检查文件的大小和类型是否符合要求
            if (file.Length > 1024 * 1024 * 2 || !file.ContentType.StartsWith("image/"))
            {
                return BadRequest("文件大小或类型不符合要求。");
            }

            // 保存文件到服务器上的指定文件夹中
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", file.FileName);
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var result = new { url = $"http://{Request.Host}/images/{file.FileName}" };
            // 返回上传成功的响应结果
            return Ok(new { success = true, message = "文件上传成功。", result });
        }

        /// <summary>
        /// 企业信息添加
        /// </summary>
        /// <param name="info_Company"></param>
        /// <returns></returns>
        [HttpPost]
        public bool AddComploy([FromBody] Info_company info_Company)
        {
            return company.AddCompany(info_Company);
        }

        /// <summary>
        /// 企业信息展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Info_company GetCompany()
        {
            return company.GetCompany();
        }

        /// <summary>
        /// 企业信息修改
        /// </summary>
        /// <param name="companys"></param>
        /// <returns></returns>
        [HttpPut]
        public bool UpdCompany([FromBody] Info_company info_Company)
        {
            return company.UpdCompany(info_Company);
        }

    }
}
